package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.StrategyCatologVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQueryObject qo) {
        Page<Strategy> list = strategyService.query(qo);
        model.addAttribute("page", list);
        return "strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy) {
        // 拿到攻略分类
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        // 设置分类的名字   catalogName
        strategy.setCatalogName(strategyCatalog.getName());
        // 设置目的地的Id   destId
        strategy.setDestId(strategyCatalog.getDestId());
        // 设置目的地的名字 destName
        strategy.setDestName(strategyCatalog.getDestName());
        // 设置主题名字     themeName
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());

        // 判断是否是国内还是国外
        // 通过找到攻略的目的地, 并且在目的地中不断找父目的地, 判断是否最后那个是中国, 国为最高, 所以只需要获取第一个即可
        List<Destination> dests = destinationService.queryToast(strategyCatalog.getDestId());
        if(!"中国".equals(dests.get(0).getName())) {
            strategy.setIsabroad(1);
        }

        if(!StringUtils.hasLength(strategy.getId())) {
            strategy.setCreateTime(new Date());
            strategyService.save(strategy);
        } else {
            // 修改之前 将表单中没有涉及的字段重新设置到攻略strategy中
            Strategy temp = strategyService.get(strategy.getId());
            // 设置创建时间
            strategy.setCreateTime(temp.getCreateTime());
            // 设置viewnum
            strategy.setViewnum(temp.getViewnum());
            // 设置replynum
            strategy.setReplynum(temp.getReplynum());
            // 设置sharenum
            strategy.setSharenum(temp.getSharenum());
            // 设置thumbsupnum
            strategy.setThumbsupnum(temp.getThumbsupnum());
            // 设置favornum
            strategy.setFavornum(temp.getFavornum());
            strategyService.update(strategy);
        }
        return JsonResult.success();
    }

    @RequestMapping("/input")
    public String input(Model model, String id) {
        if (StringUtils.hasLength(id)){
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy", strategy);
        }

        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);

        List<StrategyCatologVO> catalogs = strategyService.queryStrategyCatolog();
        model.addAttribute("catalogs", catalogs);

        return "strategy/input";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        strategyService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state, String id) {
        strategyService.changeState(state, id);
        return JsonResult.success();
    }
}
